/*
 File: BuddyListViewController.m
 //  HelloWorld
 //
 //  Created by Martin Le C. on 6/7/09.
 //  Copyright 2009 __MyCompanyName__. All rights reserved.
 
 */
#import "BuddyCell.h"
#import "YahooUser.h"
#import "YahooGroup.h"
#import "BuddyListViewController.h"
#import "HelloWorldAppDelegate.h"
@implementation BuddyListViewController

@synthesize dataArray;

- (void)viewDidLoad {
	self.navigationItem.hidesBackButton = YES;
	self.navigationItem.title = @"Buddy List";
	self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
																							target:self action:@selector(signOut:)] autorelease];
	self.navigationItem.leftBarButtonItem.title = @"Sign Out";
}

- (void)dealloc
{	
    [dataArray release];
	[super dealloc];
}
#pragma mark - Sign Out 
- (IBAction)signOut:(id)sender {
	// open a alert with an OK and cancel button  
	UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"Do you really want to sign out?" message:nil delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];  
	[alert show];  
	[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
	if (buttonIndex == 1) {  
	// Really sign out!  
		HelloWorldAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
		[appDelegate cancelSignIn];
	} else {  
	// Don't signout  
	}  
}  
#pragma mark - UIViewController delegate methods

- (void)didReceiveMemoryWarning
{
	// invoke super's implementation to do the Right Thing, but also release the input controller since we can do that	
	// In practice this is unlikely to be used in this application, and it would be of little benefit,
	// but the principle is the important thing.
	//
	[super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}
- (void) receiveMsg:(NSString*)buddyId {

}

#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	//Number of rows it should expect should be based on the section
	YahooGroup *group = [dataArray objectAtIndex:section];
	return [group countUsers];
}

//RootViewController.m
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [[dataArray objectAtIndex:section] name];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	// find the cell being touched and update its checked/unchecked image
	BuddyCell *targetCustomCell = (BuddyCell *)[tableView cellForRowAtIndexPath:indexPath];
	[targetCustomCell checkAction:nil];
	
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCustomCellID = @"BuddyCellID";
	BuddyCell *cell = (BuddyCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
	if (cell == nil)
	{
		cell = (BuddyCell *)[[[BuddyCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCustomCellID] autorelease];
	}
	//First get the dictionary object
	YahooGroup *yg = [dataArray objectAtIndex:indexPath.section];		
	YahooUser* u = [yg getUserAt:indexPath.row];
	NSLog(@"%@", u.yahooId);
	if (indexPath.row%2 == 0) {
		NSLog(@"%i", indexPath.row%2);
		cell.backgroundColor = [UIColor grayColor];
	}
	cell.username = u.yahooId;
	UIImage *image = [[UIImage alloc] init];
	switch (u.status) {
		// available
		case 0:
			image = [UIImage imageNamed:@"available.png"];
			cell.image = image;
		// busy, step out tect
		case 1:
		case 2:
			image = [UIImage imageNamed:@"busy.png"];
			cell.image = image;
		// invisible, unknown
		default: 
			image = [UIImage imageNamed:@"invisible.png"];
			cell.image = image;
	}
	return cell;
}

@end

